print('hhu ag py client.')
import random 
import time
import paho.mqtt.client as mqtt
import json
import uuid
 
class TRpc:
    def __init__(self) -> None:
        self.version="rpc-hhu-2023"
        self.msgId = f'{str(uuid.uuid4()).replace("-","")}'
        self.method=""
        self.params = {}
        self.exDatas= {}
class TRpcMqConfig:
    def __init__(self) -> None:
        self.broker = "dq.hhdata.cn"
        self.port = 42024
        self.userName = "admin"
        self.pwd = "public"
class TRpcMqEvent:
    def __init__(self) -> None:
        pass
    def onConnected(self , client, userdata, flags, rc):
        try:
            print('mq events - onConnected')
        except Exception as er:
            print('mq events error :onConnected.')
    def onDisConnected(self , client, userdata, rc):
        try:
            print('mq events - onDisConnected')
        except Exception as er:
            print('mq events error :onDisConnected.')
    def onMessage(self , client, userdata, message):
        try:
            print('mq events - onMessage')
        except Exception as er:
            print('mq events error :onMessage.')
    def onRpcMessage(self , client, userdata, message , rpcMessage):
        try:
            print('mq events - onRpcMessage')
        except Exception as er:
            print('mq events error :onRpcMessage.')
class TRpcMqClient:
    def __init__(self) -> None:
        self.clientId = ""
        self.mqClient = None
        self.rpcConfig = TRpcMqConfig()
        self.events = TRpcMqEvent() 
    def close(self):
        print('rpc mqclient close...')
        try:            
            if self.mqClient!=None and self.mqClient.is_connected():
                self.mqClient.disconnect()
            self.mqClient = None
        except Exception as er:
            print('rpc mqclient close error')
    def connect(self):
        print('rpc mqclient connect...')
        try:
            self.close();
            tm = str(int(time.time()));
            clientId = f'mid{tm}'            
            client = mqtt.Client( client_id= clientId, clean_session=True, userdata=None, transport="tcp" )
            client.username_pw_set(self.rpcConfig.userName, password=self.rpcConfig.pwd)
            client.on_connect = self.on_connect    #连接broker时broker响应的回调
            client.on_message = self.on_message    #接收到订阅消息时的回调
            client.on_disconnect = self.on_disconnect    #连接断开broker时broker响应的回调
            self.mqClient = client
            self.mqClient.connect(host = self.rpcConfig.broker, port=self.rpcConfig.port, keepalive=60)
            self.mqClient.loop_start()
        except Exception as er:
            print('rpc mqclient connect error')

    def open(self):
        print('rpc mqclient open...')
        try:
            pass
        except Exception as er:
            print('rpc mqclient open error') 
    def on_connect(self , client, userdata, flags, rc):
        try:
            if rc == 0 : 
                print('on_connect success')
                if self.events!=None and self.events.onConnected!=None:
                    self.events.onConnected(client, userdata, flags, rc)
            else: 
                print('on_connect faild')
        except Exception as er:
            print('on_connect error')
    def on_disconnect(self , client, userdata, rc):
        try: 
            print( 'mqclient is discconnected') 
            if self.events!=None and self.events.onDisConnected!=None:
                self.events.onDisConnected( client, userdata, rc)
        except Exception as er:
            print('on_disconnect error')
    def on_message(self , client, userdata, message):
        try:
            if self.events!=None and self.events.onMessage!=None:
                self.events.onMessage(client, userdata, message)
                try:
                    msgContent = str(message.payload)
                    rpcMessage = json.loads(msgContent)
                    if 'version' in rpcMessage and self.events.onRpcMessage!=None:
                        rpcMessage["exData"]["topic"] = message.topic
                        self.events.onRpcMessage(client, userdata, message, rpcMessage)
                except Exception as err:
                    print('')
            
        except Exception as er:
            print('on_message error') 

mq = TRpcMqClient()
mq.connect()
print(mq.clientId)